import { defineStore } from "pinia";
import type { LovesI } from "@/types";
import { reactive, ref, computed } from "vue";
import axios from "axios";
import { nanoid } from "nanoid";

// let list = reactive<LovesI>([
//   {
//     id: nanoid(),
//     title: "2024-0206",
//   },
// ]);
/* 选项式 */
// export const useLoveStore = defineStore("love", {
//   state() {
//     return {
//       sum: 0,
//       school: "school",
//       // null.unshift(item);
//       list: JSON.parse(localStorage.getItem("list") as string) || list,
//     };
//   },
//   actions: {
//     increment() {
//       // 公共逻辑
//       if (this.sum < 10) {
//         this.sum += 1;
//       }
//     },
//     async addLove() {
//       // 连续解构，赋值，重命名
//       let {
//         data: { content: title },
//       } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");
//       let item = { id: nanoid(), title };
//       this.list.unshift(item);
//     },
//   },
//   getters: {
//     bigSum: (state): number => state.sum * 10,
//     upSchool(state): string {
//       return this.school.toUpperCase();
//     },
//   },
// });
/* 组合式 */
export const useLoveStore = defineStore("love", () => {
  let sum = ref(0);
  let school = ref("school");
  // null.unshift(item);
  let list = reactive<LovesI>(
    JSON.parse(localStorage.getItem("list") as string) || []
  );
  let increment = () => {
    // 公共逻辑
    if (sum.value < 10) {
      sum.value += 1;
    }
  };
  let addLove = async () => {
    // 连续解构，赋值，重命名
    let {
      data: { content: title = "haha" },
    } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");
    let item = { id: nanoid(), title };
    list.unshift(item);
  };
  let bigSum = computed(() => {
    return sum.value * 10;
  });
  let upSchool = computed(() => {
    return school.value.toUpperCase();
  });

  return {
    sum,
    school,
    list,
    increment,
    addLove,
    bigSum,
    upSchool,
  };
});
